package com.xcfw.dorm.seviceimpl.dormassistant;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xcfw.dorm.entity.Examine;
import com.xcfw.dorm.entity.Leave;
import com.xcfw.dorm.entity.NumEnum;
import com.xcfw.dorm.entity.ReturnJson;
import com.xcfw.dorm.exception.ServiceException;
import com.xcfw.dorm.mapper.LeaveMapper;
import com.xcfw.dorm.sevice.BaseService;
import com.xcfw.dorm.sevice.ZSGCService;
import com.xcfw.dorm.util.DormUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: Dorm
 * @description: 请假
 * @author: Bruse Queen
 * @create: 2018-04-08 10:26
 **/
@Service("leaveServiceImpl")
public class LeaveServiceImpl extends BaseService implements ZSGCService {
    @Autowired
    private LeaveMapper mapper;

    @Override
    public void add(Object o) {
        Leave param = (Leave) o;
        param.setUuid(DormUtil.getUUID());
        zsgcStatus = mapper.insertSelective(param);
        if (1 != zsgcStatus) {
            throw new ServiceException("新增请假信息失败！");
        }
    }

    @Override
    public void delete(Integer id) {
        zsgcStatus = mapper.deleteByPrimaryKey(id);
        if (1 != zsgcStatus) {
            throw new ServiceException("删除请假信息失败！");
        }
    }

    @Override
    public void update(Object o) {
        Leave param = (Leave) o;

        Leave temp = mapper.selectByPrimaryKey(param.getId());
        if (temp != null) {
            if (param.equals(temp)) {
                throw new ServiceException("您未修改任何信息！");
            }
        }

        zsgcStatus = mapper.updateByPrimaryKeySelective(param);
        if (1 != zsgcStatus) {
            throw new ServiceException("修改请假信息失败！");
        }
    }

    @Override
    public Object select(Object o) {
        Leave param = (Leave) o;
        Integer studentId = param.getStudentId();
        Integer leaveType = param.getLeaveType();

        if ("".equals(param.getBuildingDormitory())) {
            param.setBuildingDormitory(null);
        }
        if ("".equals(param.getStudentName())) {
            param.setStudentName(null);
        }
        if ("".equals(param.getStudentClass())) {
            param.setStudentClass(null);
        }
        if ("".equals(param.getCreateTime())) {
            param.setCreateTime(null);
        }

        ReturnJson result;
        if (param.getPageNum() == null || param.getPageSize() == null) {
            List<Leave> list = mapper.selectActionSql(param.getBuildingDormitory(),
                    DormUtil.setNull(studentId), param.getStudentName(),
                    param.getStudentClass(), DormUtil.setNull(leaveType),
                    param.getCreateTime() == null ? null : String.valueOf(param.getCreateTime()));
            if (list.size() == 0) {
                throw new ServiceException("暂无相关数据！");
            }
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "请假信息查询成功！", list, list.size());
        } else {
            PageHelper.startPage(param.getPageNum(), param.getPageSize());
            Page<Leave> list = (Page<Leave>) mapper.selectActionSql(param.getBuildingDormitory(),
                    DormUtil.setNull(studentId), param.getStudentName(),
                    param.getStudentClass(), DormUtil.setNull(leaveType),
                    param.getCreateTime() == null ? null : String.valueOf(param.getCreateTime()));
            if (list.size() == 0) {
                throw new ServiceException("暂无相关数据！");
            }
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "请假信息查询成功！", list, list.getTotal());
        }
        return result;
    }
}
